package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.service.FeignHystrixOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping(value = "/consumer")
@DefaultProperties(defaultFallback = "hystrix_global_fallback_method",commandProperties = {})   //默认全局配置
public class FeignHystrixController {

    @Resource
    private FeignHystrixOrderService feignHystrixOrderService;

    @GetMapping("/payment/hystrix/OK/{id}")
    //@HystrixCommand            //这个标签表明该方法适用于服务降级,单个使用的话（没有指定fallbackMethod属性）是全局服务降级
    public String paymentInfo_OK(@PathVariable Long id){
        //int i=10/0;
        String str=feignHystrixOrderService.paymentInfo_OK(id);
        //打印日志
        log.info(str);
        return str;
    }

    @GetMapping("/payment/hystrix/Timeout/{id}")
    @HystrixCommand(fallbackMethod = "requestTimeoutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })      //意思就是在请求远程服务时，如果请求时间超过了设定的1.5s，就执行兜底方法（就是消费者不想等了，稍后再试）
    public String paymentInfo_Timeout(@PathVariable Long id){
        return feignHystrixOrderService.paymentInfo_Timeout(id);
    }
    //兜底方法
    public String requestTimeoutFallbackMethod(@PathVariable("id") Long id){
        return "我消费者方在远程访问时超时，系统繁忙请稍后再试,Id: "+id+"/(ㄒoㄒ)/~~";
    }
    //默认的兜底方法,该方法不能有参数，否则会报错
    public String hystrix_global_fallback_method(){
        return "服务器出错，请稍后再试,Id: /(ㄒoㄒ)/~~default_method";
    }
}
